<!DOCTYPE html>
<html lang="zh-CN">
  <head>
	  <%layout("/WEB-INF/view/common/inc/admin_head.html",{title:'选择组织机构', styles:'bootstrap,font-awesome,animate,style,ztree,dtvalidate'}){%>
	  <style type="text/css">
		  body{padding:10px;}
		  #groupTree{
			  height: 360px;
			  overflow-y: auto;
		  }
	  </style>
	  <%}%>
  </head>
  <body>
	<!-- START 内容部分 -->
	<div class="container">
		<form class="form-horizontal" id="info-form" method="post">
			<div class="form-group">
				<label class="col-sm-2 control-label text-right">组织机构树</label>
				<div class="col-sm-10">
					<ul id="groupTree" class="ztree"></ul>
				</div>
			</div>
			<div class="form-group">
				<div class="col-sm-12 text-center">
					<button type="button" class="save-btn btn btn-primary">
						<i class="fa fa-save"></i> 确定
					</button>
					<div class="row" id="tip-info"></div>
				</div>
			</div>
		</form>
	</div>
	<!-- END 内容部分 -->

	<%layout("/WEB-INF/view/common/inc/admin_js.html",{modules:'jquery,bootstrap,ztree,ztree-excheck,dtvalidate'}){}%>

	<script>
	var treeObj,selectedName,selectedPid,indexid,groupIdInput = '${groupIdInputId}',groupNameInput = '${groupNameInputId}';
	$(function(){
		//初始化验证器
		cvalidator = $.fn.dlshouwen.validator.init($('#info-form'));
		
		//提交表单
		$('.save-btn').on('click',function(){
			
			if(treeObj.getCheckedNodes(true).length==0){
				$.fn.dlshouwen.validator.tools.toast('请选择组织机构！', 'error', 1500);
				return;
			}

			<% if( inlayer == "y" ){
			%>
				//将信息传递给上级页面
				var body = parent.layer.getChildFrame('body', indexid);
				body.find('#'+groupIdInput).val(selectedPid);
				body.find('#'+groupNameInput).val(selectedName);

				parent.layer.close(parent.layer.getFrameIndex(window.name));
			<%}else{%>
				//将信息传递给上级页面
				$('#'+groupIdInput, window.parent.document).val(selectedPid);
				$('#'+groupNameInput, window.parent.document).val(selectedName);

				parent.layer.close(parent.layer.getFrameIndex(window.name));
			<%} %>

		});
		
		//组织机构树配置
		var setting = {
				check: {
					enable: true,
					chkStyle: "radio",
					radioType: "all"
				},
				data: {
					simpleData: {
						enable: true
					}
				},
				callback: {
					onClick: clickGroup,
					onCheck: checkGroup
				}
		};
		<%
			//禁止选择的机构类型
			var disabledGroupType = strutil.split(disabletypes,",");
		%>

		var zNodes =[
			<%for(group in groups){%>
			    { id: ${group.id}, pId: ${group.groupPid}, name:"${group.groupName}", <% for(dtype in disabledGroupType){ if( !isEmpty(dtype) && parseInt(dtype) == group.groupType ){ print("chkDisabled:true,"); } }%>  open: true}<%if( !groupLP.last ){print(",");}%>
			<%}%>
		]
		treeObj = $.fn.zTree.init($("#groupTree"), setting, zNodes);

		<%if(gid > 0){%>
		var node = treeObj.getNodeByParam("id", ${gid}, null);
		treeObj.checkNode(node, true, true);
		checkGroup(null,null,node);
		<%}%>

		//选择节点事件
		function checkGroup(event, treeId, treeNode){
			selectedPid = treeNode.id;
			selectedName = treeNode.name;
			$('#pid').val(selectedPid);
			$('#tip-info').html('<h4>当前已选</h4>'+selectedName);
		}

		//点击节点
		function clickGroup(event, treeId, treeNode){
			treeObj.checkNode(treeNode, true, true);
			checkGroup(event, treeId, treeNode);
		}

    });
    </script>
  </body>
</html>